home *** CD-ROM | disk | FTP | other *** search
- /* CPROG5.CPP - SOME EXAMPLES OF IF AND IF/ELSE
- Compile and run with Ctrl+F9, the press Alt+5 to see the output
- */
-
- #include <stdio.h>
- #include <conio.h>
- #include <dos.h> // Contains prototype for the sound() and other
- // functions used in the 'if (x == y)' case below.
- #include <stdlib.h> // Contains the defintion of random() which is
- // not a function, but a #define macro. Also
- // contains the protype for randomize();
- #include <time.h> // See help on randomize() for explanation
-
-
- main()
- {
- int x, y; // Shorthand for int x; followed by int y;
- randomize(); // Initialise the random number generator with a random number
- clrscr(); // Clear the screen
- x=random(100); // Get random integers in the range 0-99 into
- y=random(100); // variables x and y. See help system for more
- // info on random().
-
- printf("X is %d and Y is %d\n", x, y);
- /* Note how that with two %d codes in the string
- printf() expects two integers after it. You may
- find it confusing that the text of the article
- implies a strict definition of the number of
- and type of parameters a function takes, but
- printf() seems to be able to handle different
- numbers and types of variable. There is a
- mechanism whereby you can set up a function to
- handle variable amounts and types of input, but
- that is beyond the scope of this series. */
-
- delay(2000); // Delay 2 seconds
-
- if ( x < y )
- printf("X is smaller than Y\n");
- else
- printf("X is larger than or equal to Y\n");
-
- /* Following an IF with an ELSE gives alternative
- courses of action, depending on the result of the
- test. */
-
- delay(2000);
- x=y; // Force x=y so you can see the next bit
- // working first time round.
- if ( x == y )
- {
- printf("Amazing - X and Y are both equal\n");
- sound(700); // Sound a 700Hz tone until further notice
- delay(1000); // Sit around doing nothing for a second
- nosound(); // And turn off the sound
- }
- /* Several commands are executed if x and y are
- equal. The bottom three were highlighted, copied
- and pasted out of the example given under sound()
- in the help system, then edited.
- Remember - put the cursor on a function name or
- C++ keyword, press shift+F1, and you are taken
- to the appropriate entry in Help. */
- delay(2000);
- x=random(100); // Change x so the next bit works...
-
- /* Naughty bit coming up! For clear code
- #defines should be at the head of the
- program or grouped in a header file.
- There's nothing to stop you putting a
- #define down here, but it makes for messy
- code that's harder to debug. Don't do it! */
-
- #define BEEP sound(200); delay(500); nosound();
- /* This #define makes subsequent uses
- of BEEP expand into everything that
- follows it on the same line. */
-
- if( x != y)
- {
- printf("Sorry, my mistake, they're different!\n");
- BEEP
- }
- else
- {
- printf("I can confirm that they are still equal!\n");
- BEEP
- }
- /* A few things to note here...
- - Once again, braces are used to group
- several actions where, without them,
- only the first one would be executed.
- - Each instance of BEEP is expanded
- by the preprocessor into the three
- function calls listed in the #define line.
- Since ; marks the end of a command it is
- Ok to put several calls on one line.
- - BEEP hasn't got a semi-colon - the one from
- nosound(); does the job. Had that been
- missing, you would need to write BEEP;
- - If BEEP had been the only action after the
- IF or the ELSE, it would still need braces
- because it expands to three commands.
- - I tend to use capitals to indicate macro
- names.*/
-
- delay(5000); // Wait 5 seconds
- }
-